//The MIT License

//Copyright (c) 2009 Lluis Sanchez, Nick Darnell

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Reflection;
using System.Reflection.Emit;
using Polymorph.Properties;

namespace Polymorph
{
    /// <summary>
    /// The <see cref="CodeSelfIncrement"/> class represents the ++ operator.
    /// </summary>
    public class CodeSelfIncrement : CodeExpression
    {
        private CodeValueReference m_exp;

        /// <summary>
        /// Initializes a new instance of the <see cref="CodeSelfIncrement"/>.
        /// </summary>
        /// <param name="exp">The expression to increment.</param>
        public CodeSelfIncrement(CodeValueReference exp)
        {
            m_exp = exp;

            if (!exp.IsNumber)
            {
                throw new InvalidOperationException(
                    string.Format(Resources.Operator0CanNotBeAppliedTo1, 
                    "++", exp.ResultType.FullName));
            }
        }

        /// <summary>
        /// Generates the IL code for the increment operator.
        /// </summary>
        /// <param name="gen">The IL generator.</param>
        public override void Generate(ILGenerator gen)
        {
            m_exp.Generate(gen);
            Type t = m_exp.ResultType;
            switch (Type.GetTypeCode(t))
            {
                case TypeCode.Byte:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    gen.Emit(OpCodes.Conv_U1);
                    break;

                case TypeCode.Decimal:
                    MethodInfo met = typeof(Decimal).GetMethod("op_Increment");
                    CodeGenerationHelper.GenerateMethodCall(gen, null, met, m_exp);
                    break;

                case TypeCode.Double:
                    gen.Emit(OpCodes.Ldc_R8, 1);
                    gen.Emit(OpCodes.Add);
                    break;

                case TypeCode.Int16:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    gen.Emit(OpCodes.Conv_I2);
                    break;

                case TypeCode.UInt32:
                case TypeCode.Int32:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    break;

                case TypeCode.UInt64:
                case TypeCode.Int64:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    gen.Emit(OpCodes.Conv_U8);
                    break;

                case TypeCode.SByte:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    gen.Emit(OpCodes.Conv_I1);
                    break;

                case TypeCode.Single:
                    gen.Emit(OpCodes.Ldc_R4, 1);
                    gen.Emit(OpCodes.Add);
                    break;

                case TypeCode.UInt16:
                    gen.Emit(OpCodes.Ldc_I4_1);
                    gen.Emit(OpCodes.Add);
                    gen.Emit(OpCodes.Conv_U2);
                    break;
            }
            m_exp.GenerateSet(gen, m_exp);
        }

        /// <summary>
        /// Prints the increment operator in a human readable form.
        /// </summary>
        /// <param name="cw">The code writer to use.</param>
        public override void PrintCode(CodeWriter cw)
        {
            m_exp.PrintCode(cw);
            cw.Write("++");
        }

        /// <summary>
        /// Gets the resulting type of the increment operator.
        /// </summary>
        public override Type ResultType
        {
            get { return m_exp.ResultType; }
        }
    }
}